<template>
  <div>
    <el-form
      ref="addFormRef"
      :rules="rules"
      :model="addForm.data"
      label-width="120"
    >
      <el-form-item label="路段编号" prop="code">
        <el-input
          v-model="addForm.data.code"
          style="width: 320px"
          placeholder="请输入路段编号"
        />
      </el-form-item>
      <el-form-item label="路段名称" prop="name">
        <el-input
          v-model="addForm.data.name"
          style="width: 320px"
          placeholder="请输入路段名称"
        />
      </el-form-item>

      <el-form-item label="所属行政区" prop="reginId">
        <el-cascader
          v-model="addForm.data.reginId"
          :options="reginId.data"
          filterable
          style="width: 320px"
          placeholder="请选择所属行政区"
          :props="{
            value: 'id',
            label: 'name',
            children: 'nodes',
          }"
          @change="handleCascaderChange"
          clearable
        />
      </el-form-item>
      <el-form-item label="路段类别" prop="category">
        <el-select
          v-model="addForm.data.category"
          filterable
          style="width: 320px"
          placeholder="请选择路段类别"
        >
          <el-option
            v-for="item in category.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="路段经纬度"
        prop="longLatItude"
        style="margin-bottom: -6px !important"
      >
        <el-form-item prop="longitude">
          <el-input
            v-model="addForm.data.longitude"
            style="width: 150px; margin-right: 20px"
            placeholder="请输入路段经度"
          />
        </el-form-item>
        <el-form-item prop="latitude">
          <el-input
            v-model="addForm.data.latitude"
            style="width: 150px"
            placeholder="请输入路段纬度"
          />
        </el-form-item>
      </el-form-item>
      <el-form-item label="泊位分布" prop="distribution">
        <el-radio-group v-model="addForm.data.distribution">
          <el-radio
            v-for="item in distribution.data"
            :key="item.id"
            :value="item.id"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="泊位形式" prop="berthForm">
        <el-radio-group v-model="addForm.data.berthForm">
          <el-radio
            v-for="item in berthForm.data"
            :key="item.id"
            :value="item.id"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)"
              >重 置</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getCanton, getDictionary } from "@/service/modules/publicModule";
import { getAddRoadSection } from "../request.js";

const reginId = reactive({
  data: [],
});

const category = reactive({
  data: [],
});

const distribution = reactive({
  data: [],
});

const berthForm = reactive({
  data: [],
});

const getData = async () => {
  await getCanton().then((res) => {
    reginId.data = res.data.result;
  });
  await getDictionary("road_section_category").then((res) => {
    category.data = res.data.result;
  });
  await getDictionary("road_berth_distribution").then((res) => {
    distribution.data = res.data.result;
  });
  await getDictionary("road_berth_form").then((res) => {
    berthForm.data = res.data.result;
  });
};
getData();

const addFormRef = ref("");

const addForm = reactive({
  data: {
    longLatItude: 1,
  },
});

const handleCascaderChange = (value) => {
  console.log(value);
  if (value !== null && value.length > 0) {
    addForm.data.reginId = value[value.length - 1];
  } else {
    addForm.data.reginId = "";
  }
};

const rules = reactive({
  code: [{ required: true, message: "请输入路段编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入路段名称", trigger: "blur" }],
  reginId: [{ required: true, message: "请选择所属行政区", trigger: "change" }],
  category: [{ required: true, message: "请选择路段类别", trigger: "change" }],
  longLatItude: [{ required: true, message: "", trigger: "blur" }],
  longitude: [{ required: true, message: "请输入路段经度", trigger: "blur" }],
  latitude: [{ required: true, message: "请输入路段纬度", trigger: "blur" }],
  distribution: [
    { required: true, message: "请选择泊位分布", trigger: "change" },
  ],
  berthForm: [{ required: true, message: "请选择泊位形式", trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getAddRoadSection(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  formEl.resetFields();
};
const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>